package com.da.lon.wang.xlg.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ant.liao.GifView;
import com.ant.liao.GifView.GifImageType;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.view.ViewFinder;

public abstract class BaseFragment extends Fragment {
    public Activity mFragmentActivity;
    protected View mRootView;
    protected boolean initAble = true;
    protected ViewFinder mViewFinder;
    private View dialogView;
    private GifView dialogGifView;
    private Dialog dialog;
    private ConnectionChangeReceiver mReceiver;

    public BaseFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialogView = LayoutInflater.from(getActivity()).inflate(
                R.layout.ocoffee_loading_dialog, null);
        dialogGifView = (GifView) dialogView.findViewById(R.id.loading_gif);
        dialogGifView.setGifImage(R.drawable.gif);
        dialogGifView.setGifImageType(GifImageType.COVER);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        this.initAble = this.mRootView == null;
        if (this.initAble) {
            this.mRootView = this.inflateView(inflater, container);
        }

        this.mViewFinder = new ViewFinder(this.mRootView);
        this.parentIsAdded();
        if (this.initAble) {
            this.initView();
        }

        return this.mRootView;
    }

    protected abstract View inflateView(LayoutInflater inflater, ViewGroup parent);

    protected abstract void initView();

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mFragmentActivity = activity;
    }

    protected void parentIsAdded() {
        try {
            if (null != this.mRootView) {
                ViewGroup parent = (ViewGroup) this.mRootView.getParent();
                if (parent != null) {
                    parent.removeView(this.mRootView);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void close() {
        try {
            if (this.getFragmentManager() != null
                    && this.getFragmentManager().getBackStackEntryCount() > 0) {
                this.getFragmentManager().popBackStack();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void onDestroy() {
        super.onDestroy();
        this.mFragmentActivity = null;
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        super.onDestroy();
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    protected void setViewVisible(int layoutId, int visibility) {
        this.mViewFinder.find(layoutId).setVisibility(visibility);
    }

    protected TextView setViewText(int layoutId, CharSequence text) {
        TextView tv = (TextView) this.mViewFinder.find(layoutId);
        tv.setText(text);
        return tv;
    }

    protected void setViewClickListener(int layoutId, OnClickListener listener) {
        try {
            if (this.mViewFinder != null
                    && this.mViewFinder.find(layoutId) != null) {
                this.mViewFinder.find(layoutId).setOnClickListener(listener);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDialog() {
        try {
            if (dialog == null) {
                dialog = new Dialog(getActivity(),
                        R.style.ocoffee_loading_dialog);
                dialog.setCanceledOnTouchOutside(false);
                dialog.setContentView(dialogView);
            }
            if (dialog != null && !dialog.isShowing()) {
                dialog.show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismissDialog() {
        try {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 注册广播
     */
    protected void registerBaseReceiver() {
        IntentFilter filter = new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION);
        mReceiver = new ConnectionChangeReceiver();
        getActivity().registerReceiver(mReceiver, filter);
    }

    /**
     * 网络变化，自动刷新数据
     *
     * @author Administrator
     */
    protected abstract void RefreshData();

    /**
     * 网络监听广播
     *
     * @author Administrator
     */
    public class ConnectionChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (null != mobNetInfo && null != wifiNetInfo
                    && !mobNetInfo.isConnected()
                    && !wifiNetInfo.isConnected()) {
            } else {
                BaseFragment.this.RefreshData();
            }


        }
    }

}
